package com.wskj.library.dao.impl;

import com.wskj.library.pojo.*;

import com.wskj.library.dao.*;

import java.util.List;

/**
 * employer dao接口类实现类
 * 2018-03-22 T148
 */ 
public class EmployerDaoImpl extends BaseDao implements EmployerDao{

	 @Override
	 public int addEmployer(Employer  e){
		 String sql="insert into employer (e_number, e_account, e_pass, e_name, e_rid,e_phone,e_email,e_img,e_birthday,e_joinday)"
		 		+ " values (?,?,?,?,?, ?,?,?,?,?); ";
		 Object[] params= {e.getE_number(),e.getE_account(),e.getE_pass(),
				 e.getE_name(),e.getE_rid(),e.getE_phone(),e.getE_email(),
				 e.getE_img(),e.getE_birthday(),e.getE_joinday()};
		 return super.executeUpdate(sql, params);
	 }
	 
	 @Override
	 public int deleteById(int id){
		 String sql="delete from employer  where e_id=? ";
		 Object[] params= {id};
		 return super.executeUpdate(sql, params);
	 }
	 @Override
	 public int updateEmployer(Employer e){
		 String sql=" update employer set e_name=?,e_rid=?,e_phone=?,e_email=?,e_img=?,e_birthday=?,e_joinday=?  where e_id=?";
		 Object[] params= {e.getE_name(),e.getE_rid(),e.getE_phone(),e.getE_email(),e.getE_img(),e.getE_birthday(),e.getE_joinday(),e.getE_id()};
		 return super.executeUpdate(sql, params);
	 }
	 @Override
	 public Employer getEmployerById(int id){
		 String sql="select * from employer where e_id=? ";
		 Object[] params= {id};
		 return super.queryOne(sql, params,Employer.class);
	 }
	 @Override
	 public  List<Employer>  getAllEmployers(){
		 String sql=" select e.*,rname  " + 
		 		"  from employer e " + 
		 		"  inner join roleinfo r" + 
		 		"  on e.e_rid=r.rid ";
		 Object[] params= null;
		 return super.queryAll(sql, params, Employer.class);
	 }
	
	@Override
	public long getCountsByName(String name, String phone, String birthday) {
		String sql=" select count(*) from employer where "
				+ " e_name like CONCAT('%',?,'%') "
				+ " and e_phone like CONCAT('%',?,'%') "
				+ "  and (e_birthday =? or '' =?) ";
		Object[] params= {name,phone,birthday,birthday};
		return super.getCounts(sql, params);
	}
	
	@Override
	public List<Employer> getAllEmployersWithPage(String name, String phone, String birthday, int index, int pageSize) {
		String sql=" select * from employer where "
				+ " e_name like CONCAT('%',?,'%') "
				+ " and e_phone like CONCAT('%',?,'%') "
				+ "  and (e_birthday =? or '' =?) "
				+ " limit ?,? ;";
		Object[] params= {name,phone,birthday,birthday,(index-1)*pageSize,pageSize};
		return super.queryAll(sql, params, Employer.class);
	}
	
	
	
	@Override
	public Employer checkLogin(String account, String pass) {		
		String sql="select * from employer where e_account=? and e_pass=?";
		Object[] params= {account,pass};
		return super.queryOne(sql, params, Employer.class);
	}

	
	@Override
	public int updateUserPass(int id, String pass) {		
		String sql="update employer set e_pass=? where e_id=?";
		Object[] params= {pass,id};
		return super.executeUpdate(sql, params);
	}

	
	@Override
	public int deleteAllEmployer(String[] id) {		
		String  sql="delete from employer where e_id in (";
		for(int i=0;i<id.length;i++) {
			
			if(i == id.length-1) {
				sql+=" ?) ";
			}else {
				sql +="?,";
			}
		}
		
		Object[] params=id;
		
		return super.executeUpdate(sql, params);
	}

	
	@Override
	public int saveEmployerList(List<Employer> employers) {
		int count=0;
		 String sql="insert into employer (e_number, e_account, e_name, e_rid,e_phone,e_email,e_birthday,e_joinday)"
			 		+ " values (?,?,?,?,?, ?,?,?); ";			 
		for(Employer e:employers) {			
			Object[] params= {e.getE_number(),e.getE_account(),
					 e.getE_name(),e.getE_rid(),e.getE_phone(),e.getE_email(),
					 e.getE_birthday(),e.getE_joinday()};			
			count +=super.executeUpdate(sql, params);
		}		
		return count;
	}
	
}

